package hk.m4s.lr.repair.test.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.FlashSaleModel;
import hk.m4s.lr.repair.test.ui.goods.GoodsEventActivity;

public class SearchGoodsListAdapter extends BaseAdapter {
	private Context context;
	public List<FlashSaleModel> list;
	private LayoutInflater flater;
	public SearchGoodsListAdapter(Context context, List<FlashSaleModel> list) {
		this.context = context;
		this.list = list;
		this.flater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = flater.inflate(R.layout.list_item_goods_search, null);
			holder.order_shop_price = (TextView) convertView
					.findViewById(R.id.order_shop_price);
			holder.car_shop_name = (TextView) convertView
					.findViewById(R.id.car_shop_name);
			holder.shop_old_price = (TextView) convertView
					.findViewById(R.id.shop_old_price);
			holder.order_shop_sale = (TextView) convertView
					.findViewById(R.id.order_shop_sale);
			holder.addCar= (Button) convertView
					.findViewById(R.id.car_add);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
        if(list!=null) {
			FlashSaleModel bean = list.get(position);
			holder.car_shop_name.setText(bean.getGoodsName());
			holder.order_shop_price.setText("¥"+bean.getGoodsPrice());
			holder.shop_old_price.setText("¥"+bean.getGoodsOldPrice());
			holder.shop_old_price.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
			holder.order_shop_sale.setText(bean.getGoodsSale());
		}
		holder.addCar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent aboutIntent = new Intent(context, GoodsEventActivity.class);
				aboutIntent.putExtra("goods_id","1");
				context.startActivity(aboutIntent);
			}
		});
		return convertView;
	}

	static class ViewHolder {
		public TextView order_shop_price;
		public TextView car_shop_name;
		public TextView shop_old_price;
		public TextView order_shop_sale;
		public Button addCar;
	}
}
